package com.egao.common.module.aolan.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Db;
import cn.hutool.db.DbUtil;
import com.egao.common.core.web.JsonResult;
import com.egao.common.module.aolan.entity.AoLan;
import com.egao.common.module.student.constants.StudentSignConstants;
import com.egao.common.module.student.entity.Student;
import com.egao.common.module.student.entity.StudentSign;
import com.egao.common.module.student.mapper.StudentSignMapper;
import com.egao.common.module.student.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 奥兰服务
 *
 * @author lym
 */
@Service
public class AoLanService {

    @Autowired
    private StudentService studentService;

    @Autowired
    private AoLanSqlService aoLanSqlService;

    @Autowired
    private StudentSignMapper studentSignMapper;

    /**
     * 同步单个学生毕业去向信息到奥兰
     * 学院审核通过时执行
     *
     * @param studentSign studentSign
     */
    public JsonResult insert(StudentSign studentSign) {
        if (studentSign == null) {
            return JsonResult.error("数据异常");
        }

        AoLan aoLan = studentSignToAoLan(studentSign);
        if (aoLan == null) {
            return JsonResult.error("数据转换时异常");
        }

        return aoLanSqlService.insert(aoLan);
    }

    /**
     * 查询数据是否存在奥兰
     * @param year 毕业年份
     * @param xh 学号
     * @return true存在 false不存在
     */
    public boolean verify(Integer year, String xh) {
        return aoLanSqlService.select(year, xh);
    }

    /**
     * 删除奥兰数据
     * 解约成功、重置就业方式时执行
     * @param year 毕业年份
     * @param xh 学号
     */
    public void delete(Integer year, String xh) {
        aoLanSqlService.delete(year, xh);
    }

    /**
     * 批量同步毕业去向信息到奥兰
     *
     * @param year 毕业年份
     * @return JsonResult
     */
    public JsonResult insertBatch(Integer year) {
        TimeInterval timer = DateUtil.timer();

        List<AoLan> aoLanList = new ArrayList<>();
        List<StudentSign> studentSignList = studentSignMapper.listAll(new StudentSign());
        for (StudentSign studentSign : studentSignList) {
            aoLanList.add(studentSignToAoLan(studentSign));
        }

        JsonResult jsonResult = aoLanSqlService.insertBatch(year, aoLanList);
        System.out.println("批量同步奥兰耗时：" + timer.intervalMinute());
        return jsonResult;
    }

    /**
     * 毕业去向数据转换为aoLan数据容器
     *
     * @param studentSign
     * @return
     */
    public AoLan studentSignToAoLan(StudentSign studentSign) {
        Student student = studentService.getById(studentSign.getStudentId());
        if (student == null) {
            return null;
        }

        String signCategory = studentSign.getSignCategory();

        AoLan aoLan = new AoLan();
        aoLan.setYearId(student.getYearId());

        String xdm = !StrUtil.isBlank(student.getCollegeCode()) ? "'" + student.getCollegeCode() + "'" : "''";
        aoLan.setXdm(xdm);

        String bjhm = !StrUtil.isBlank(student.getBjhm()) ? "'" + student.getBjhm() + "'" : "''";
        aoLan.setBjhm(bjhm);

        String xh = !StrUtil.isBlank(student.getXh()) ? "'" + student.getXh() + "'" : "''";
        aoLan.setXh(xh);

        String xm = !StrUtil.isBlank(student.getXm()) ? "'" + student.getXm() + "'" : "''";
        aoLan.setXm(xm);

        // 20220530注释 梁老师要求不同步这个字段 置字符串空
        //aoLan.setByqxfldm("'" + dispatchHiredWayToAoLan(studentSign.getDispatchHiredWay()) + "'");
        aoLan.setByqxfldm("''");
        aoLan.setDatgqxdm("''");

        String byqxdm = !StrUtil.isBlank(studentSign.getDispatchGradWhere()) ? "'" + studentSign.getDispatchGradWhere() + "'" : "''";
        aoLan.setByqxdm(byqxdm);

        String bdzqflbdm = !StrUtil.isBlank(studentSign.getDispatchReportIssuedCategory()) ? "'" + studentSign.getDispatchReportIssuedCategory() + "'" : "''";
        aoLan.setBdzqflbdm(bdzqflbdm);

        String dwmc = !StrUtil.isBlank(studentSign.getDispatchReportCompany()) ? "'" + studentSign.getDispatchReportCompany() + "'" : "''";
        aoLan.setDwmc(dwmc);

        String dwdm = !StrUtil.isBlank(studentSign.getDispatchCompanyCode()) ? "'" + studentSign.getDispatchCompanyCode() + "'" : "''";
        aoLan.setDwdm(dwdm);

        String dwlsbmdm = studentSign.getDispatchCompanyMembership();
        if (!StrUtil.isBlank(dwlsbmdm)) {
            if (2 == dwlsbmdm.length()) {
                dwlsbmdm = "00" + dwlsbmdm;
            }
            if (3 == dwlsbmdm.length()) {
                dwlsbmdm = "0" + dwlsbmdm;
            }
            dwlsbmdm = "'" + dwlsbmdm+ "'";
        } else {
            dwlsbmdm = "''";
        }
        aoLan.setDwlsbmdm(dwlsbmdm);

        String dwszddm = !StrUtil.isBlank(studentSign.getDispatchReportCompanyAddress()) ? "'" + studentSign.getDispatchReportCompanyAddress() + "'" : "''";
        aoLan.setDwszddm(dwszddm);

        String dwxzdm = !StrUtil.isBlank(studentSign.getDealCompanyNature()) ? "'" + studentSign.getDealCompanyNature() + "'" : "''";
        aoLan.setDwxzdm(dwxzdm);

        String jyzkdm = !StrUtil.isBlank(studentSign.getDispatchJobCondition()) ? "'" + studentSign.getDispatchJobCondition() + "'" : "''";
        aoLan.setJyzkdm(jyzkdm);

        String jylbdm = !StrUtil.isBlank(studentSign.getDispatchJobCategory()) ? "'" + studentSign.getDispatchJobCategory() + "'" : "''";
        aoLan.setJylbdm(jylbdm);

        // 20220531 不同步未就业类别代码
        //String wjylbdm = !StrUtil.isBlank(studentSign.getWaitCategory()) ? "'" + studentSign.getWaitCategory() + "'" : "''";
        //aoLan.setWjylbdm(wjylbdm);

        String kzx1 = !StrUtil.isBlank(studentSign.getExt1()) ? "'" + studentSign.getExt1() + "'" : "''";
        aoLan.setKzx1(kzx1);

        String kzx5 = !StrUtil.isBlank(studentSign.getExt5()) ? "'" + studentSign.getExt5() + "'" : "''";
        aoLan.setKzx5(kzx5);

        String dwzzjg = !StrUtil.isBlank(studentSign.getDealCompanyOrganizationCode()) ? "'" + studentSign.getDealCompanyOrganizationCode() + "'" : "''";
        aoLan.setDwzzjg(dwzzjg);

        String dwhydm = !StrUtil.isBlank(studentSign.getDealCompanyIndustry()) ? "'" + studentSign.getDealCompanyIndustry() + "'" : "''";
        aoLan.setDwhydm(dwhydm);

        String gzzwlbdm = !StrUtil.isBlank(studentSign.getDealWorkJobCategory()) ? "'" + studentSign.getDealWorkJobCategory() + "'" : "''";
        aoLan.setGzzwlbdm(gzzwlbdm);

        String hbzdcydm = !StrUtil.isBlank(studentSign.getDealHbIndustry()) ? studentSign.getDealHbIndustry() : "0";
        hbzdcydm = !"0".equals(hbzdcydm) ? "'" + hbzdcydm + "'" : "''";
        aoLan.setHbzdcydm(hbzdcydm);


        aoLan.setPqbdzh("''");
        aoLan.setPqsj("''");
        aoLan.setShyj("''");

        String dealCompanyContactAddress = studentSign.getDealCompanyContactAddress();
        String dealWorkAddress = studentSign.getDealWorkAddress();
        String dwdz = !StrUtil.isBlank(dealCompanyContactAddress) ? dealCompanyContactAddress : dealWorkAddress;
        dwdz = !StrUtil.isBlank(dwdz) ? "'" + dwdz + "'" : "''";
        if (dwdz.length() > 100) {
            dwdz = dwdz.substring(0, 49);
        }
        aoLan.setDwdz(dwdz);

        String dwyzbm = !StrUtil.isBlank(studentSign.getDealCompanyContactZip()) ? "'" + studentSign.getDealCompanyContactZip() + "'" : "''";
        aoLan.setDwyzbm(dwyzbm);

        String dwlxr = !StrUtil.isBlank(studentSign.getDealCompanyContactName()) ? "'" + studentSign.getDealCompanyContactName() + "'" : "''";
        aoLan.setDwlxr(dwlxr);

        String dwlxfs = !StrUtil.isBlank(studentSign.getDealCompanyContactMobile()) ? "'" + studentSign.getDealCompanyContactMobile() + "'" : "''";
        aoLan.setDwlxfs(dwlxfs);

        String daclqk = !StrUtil.isBlank(studentSign.getDispatchArchivesCompany()) ? "'" + studentSign.getDispatchArchivesCompany() + "'" : "''";
        aoLan.setDaclqk(daclqk);

        String dazddz = !StrUtil.isBlank(studentSign.getDispatchArchivesAddress()) ? "'" + studentSign.getDispatchArchivesAddress() + "'" : "''";
        aoLan.setDazddz(dazddz);

        String dazdyb = !StrUtil.isBlank(studentSign.getDispatchArchivesZip()) ? "'" + studentSign.getDispatchArchivesZip() + "'" : "''";
        aoLan.setDazdyb(dazdyb);

        String dazddh = !StrUtil.isBlank(studentSign.getDispatchArchivesMobile()) ? "'" + studentSign.getDispatchArchivesMobile() + "'" : "''";
        aoLan.setDazddh(dazddh);

        String hkqx = !StrUtil.isBlank(studentSign.getDispatchHkAddress()) ? "'" + studentSign.getDispatchHkAddress() + "'" : "''";
        aoLan.setHkqx(hkqx);

        String hqtjdm = !StrUtil.isBlank(studentSign.getSurveyWay()) ? "'" + studentSign.getSurveyWay() + "'" : "''";
        aoLan.setHqtjdm(hqtjdm);

        String yxdjdm = !StrUtil.isBlank(studentSign.getSurveySalary()) ? "'" + studentSign.getSurveySalary() + "'" : "''";
        aoLan.setYxdjdm(yxdjdm);

        String qymyddm = !StrUtil.isBlank(studentSign.getSurveyCompany()) ? "'" + studentSign.getSurveyCompany() + "'" : "''";
        aoLan.setQymyddm(qymyddm);

        String gzzyxgdm = !StrUtil.isBlank(studentSign.getSurveyJob()) ? "'" + studentSign.getSurveyJob() + "'" : "''";
        aoLan.setGzzyxgdm(gzzyxgdm);

        String rzppdm = !StrUtil.isBlank(studentSign.getSurveyMatching()) ? "'" + studentSign.getSurveyMatching() + "'" : "''";
        aoLan.setRzppdm(rzppdm);

        String hdodsldm = !StrUtil.isBlank(studentSign.getSurveyOffer()) ? "'" + studentSign.getSurveyOffer() + "'" : "''";
        aoLan.setHdodsldm(hdodsldm);

        String jygzmyddm = !StrUtil.isBlank(studentSign.getSurveySchool()) ? "'" + studentSign.getSurveySchool() + "'" : "''";
        aoLan.setJygzmyddm(jygzmyddm);

        String lxgjordq = !StrUtil.isBlank(studentSign.getAbroadCountry()) ? "'" + studentSign.getAbroadCountry() + "'" : "''";
        aoLan.setLxgjordq(lxgjordq);

        String lxyxwwmc = !StrUtil.isBlank(studentSign.getAbroadSchoolEn()) ? "'" + studentSign.getAbroadSchoolEn() + "'" : "''";
        aoLan.setLxyxwwmc(lxyxwwmc);

        String lxzyzwmc = !StrUtil.isBlank(studentSign.getAbroadMajorCn()) ? "'" + studentSign.getAbroadMajorCn() + "'" : "''";
        aoLan.setLxzyzwmc(lxzyzwmc);

        String lxzywwmc = !StrUtil.isBlank(studentSign.getAbroadMajorEn()) ? "'" + studentSign.getAbroadMajorEn() + "'" : "''";
        aoLan.setLxzywwmc(lxzywwmc);

        String xlzwmcdm = !StrUtil.isBlank(studentSign.getAbroadDegree()) ? "'" + studentSign.getAbroadDegree() + "'" : "''";
        aoLan.setXlzwmcdm(xlzwmcdm);

        String hkqzlx = !StrUtil.isBlank(studentSign.getHkqzlx()) ? "'" + studentSign.getHkqzlx() + "'" : "''";
        aoLan.setHkqzlxdm(hkqzlx);

        String hkqzszd = !StrUtil.isBlank(studentSign.getHkqzszd()) ? "'" + studentSign.getHkqzszd() + "'" : "''";
        aoLan.setHkqzszddm(hkqzszd);

        String hkqzdz = !StrUtil.isBlank(studentSign.getHkqzdz()) ? "'" + studentSign.getHkqzdz() + "'" : "''";
        if (!StrUtil.isBlank(hkqzdz)) {
            aoLan.setHkqx(hkqzdz);
        }

        aoLan.setCkXys(1);
        aoLan.setCkSydtg(1);
        aoLan.setCkSbbtg(1);
        aoLan.setCkSrctg(1);
        aoLan.setCkWhsrctg(1);
        aoLan.setQtrcjgjsh(1);
        aoLan.setCkCgzm(1);
        aoLan.setCkSxcl(1);
        aoLan.setCkJyzm(1);
        aoLan.setCzsj("''");
        aoLan.setFdysp(0);
        aoLan.setFdy("''");
        aoLan.setFdyspsj("''");
        aoLan.setYxsp(0);
        aoLan.setYxspr("''");
        aoLan.setYxspsj("''");
        aoLan.setXjsp(0);
        aoLan.setXjspr("''");
        aoLan.setXjspsj("''");
        aoLan.setZydm("'" + student.getMajorCode() + "'");
        aoLan.setLqyxdm("''");

        if (StudentSignConstants.SIGN_CATEGORY_ZYZY.equals(signCategory)) {
            String address = studentSign.getFreeWorkAddress();
            address = !StrUtil.isBlank(address) ? "'" + address + "'" : "''";
            aoLan.setSjdwszddm(address);
        } else if (StudentSignConstants.SIGN_CATEGORY_SX.equals(signCategory)) {
            aoLan.setLqyxdm(studentSign.getStudySchoolName());

            String address = studentSign.getStudySchoolAddress();
            address = !StrUtil.isBlank(address) ? "'" + address + "'" : "''";
            aoLan.setSjdwszddm(address);

            String name = studentSign.getDispatchCompanyName();
            name = !StrUtil.isBlank(name) ? "'" + name + "'" : "''";
            aoLan.setDwmc(name);
        } else {
            String address = studentSign.getDealCompanyAddress();
            address = !StrUtil.isBlank(address) ? "'" + address + "'" : "''";
            aoLan.setSjdwszddm(address);
        }

        if (StudentSignConstants.SIGN_CATEGORY_DJY.equals(signCategory)) {
            aoLan.setJylbdm(studentSign.getWaitCategory());
        }


        return aoLan;
    }

    /**
     * 录用方式转奥兰毕业去向分类代码
     *
     * @param dispatchHiredWay
     * @return
     */
    private String dispatchHiredWayToAoLan(String dispatchHiredWay) {
        String aoLanCode = "";

        switch (dispatchHiredWay) {
            case StudentSignConstants.LYFS_01:
            case StudentSignConstants.LYFS_04:
                aoLanCode = "11";
                break;
            case StudentSignConstants.LYFS_02:
                aoLanCode = "13";
                break;
            case StudentSignConstants.LYFS_03:
            case StudentSignConstants.LYFS_06:
                aoLanCode = "10";
                break;
            case StudentSignConstants.LYFS_05:
                aoLanCode = "14";
                break;
            case StudentSignConstants.LYFS_07:
            case StudentSignConstants.LYFS_16:
            case StudentSignConstants.LYFS_18:
                aoLanCode = "51";
                break;
            case StudentSignConstants.LYFS_08:
                aoLanCode = "53";
                break;
            case StudentSignConstants.LYFS_09:
            case StudentSignConstants.LYFS_17:
            case StudentSignConstants.LYFS_20:
                aoLanCode = "54";
                break;
            case StudentSignConstants.LYFS_10:
                aoLanCode = "65";
                break;
            case StudentSignConstants.LYFS_11:
            case StudentSignConstants.LYFS_12:
            case StudentSignConstants.LYFS_14:
                aoLanCode = "40";
                break;
            case StudentSignConstants.LYFS_13:
                aoLanCode = "41";
                break;
            case StudentSignConstants.LYFS_15:
            case StudentSignConstants.LYFS_19:
                aoLanCode = "55";
                break;
            case StudentSignConstants.LYFS_21:
                aoLanCode = "30";
                break;
            case StudentSignConstants.LYFS_22:
                aoLanCode = "34";
                break;
            case StudentSignConstants.LYFS_23:
                aoLanCode = "60";
                break;
            case StudentSignConstants.LYFS_24:
            case StudentSignConstants.LYFS_25:
                aoLanCode = "64";
                break;
            default:
                break;
        }

        return aoLanCode;
    }
}
